//*****************************************************************************
// 1. Års eksamen @ Datamatiker - EASJ
// Holmegaard Heldagsskole Administrations system 
// 
// Denne klasse er udarbejdet af: Marc og Kasper
//*****************************************************************************
package view.elev;

import control.ControlUtil;
import javax.swing.DefaultListModel;
import model.*;

public class PotentialeKompetancePanel extends javax.swing.JPanel {

    private ControlUtil controlUtil;
    private DefaultListModel trinmaalModel;
    private DefaultListModel potentialeModel;
    private DefaultListModel kompetenceModel;

    public PotentialeKompetancePanel(ControlUtil controlUtil) {

        this.controlUtil = controlUtil;
        trinmaalModel = new DefaultListModel();
        potentialeModel = new DefaultListModel();
        kompetenceModel = new DefaultListModel();


        initComponents();
        tilfojprotet.setEnabled(false);

    }

    public void hentElever() {

        int value = vaelgElevCombo.getSelectedIndex();
        vaelgElevCombo.removeAllItems();
        vaelgElevCombo.setEnabled(true);
        vaelgElevCombo.addItem("Vælg elev");
        Elev[] el = controlUtil.getElevHandler().hentElever();
        for (int i = 0; i < el.length; i++) {
            vaelgElevCombo.addItem(el[i]);
        }
        vaelgElevCombo.setSelectedIndex(value);
    }

    private void rydFelter() {
        while (potentialeModel.size() > 0) {
            potentialeModel.remove(0);
        }
        while (kompetenceModel.size() > 0) {
            kompetenceModel.remove(0);
        }
        while (trinmaalModel.size() > 0) {
            trinmaalModel.remove(0);
        }

    }

    private void hentTrinmaal() {
        HoldFag hf = (HoldFag) elevFagCombo.getSelectedItem();
        Elev elev = (Elev) vaelgElevCombo.getSelectedItem();
        Trinmaal[] trinmaal = controlUtil.getTrinmaalHandler().hentSpecifikkeTrinmaal(elev, hf);
        trinmaalModel.removeAllElements();
        for (int i = 0; i < trinmaal.length; i++) {
            trinmaalModel.addElement(trinmaal[i]);
        }
    }

    private void hentPotentiale() {
        HoldFag hf = (HoldFag) elevFagCombo.getSelectedItem();
        Potentiale[] potentialer = controlUtil.getTrinmaalHandler().hentElevsPotentialer((Elev) vaelgElevCombo.getSelectedItem(), hf);

        for (int i = 0; i < potentialer.length; i++) {
            potentialeModel.addElement(potentialer[i]);
        }
    }

    private void hentKompetencer() {
        HoldFag hf = (HoldFag) elevFagCombo.getSelectedItem();
        Potentiale[] kompetencer = controlUtil.getTrinmaalHandler().getElevsKompetencer((Elev) vaelgElevCombo.getSelectedItem(), hf);

        for (int i = 0; i < kompetencer.length; i++) {
            kompetenceModel.addElement(kompetencer[i]);

        }
    }

    private void updatekompe() {
        rydFelter();
        hentPotentiale();
        hentKompetencer();
        hentTrinmaal();

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        vaelgElevCombo = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        elevFagCombo = new javax.swing.JComboBox();
        tilfojprotet = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        trinmaalList = new javax.swing.JList(trinmaalModel);
        jScrollPane2 = new javax.swing.JScrollPane();
        kompetenceList = new javax.swing.JList(kompetenceModel);
        jScrollPane3 = new javax.swing.JScrollPane();
        potentialeList = new javax.swing.JList(potentialeModel);
        priotetCombo = new javax.swing.JComboBox();

        setEnabled(false);
        setMaximumSize(new java.awt.Dimension(1016, 696));
        setMinimumSize(new java.awt.Dimension(1016, 696));
        setPreferredSize(new java.awt.Dimension(1016, 696));
        setLayout(null);

        jLabel1.setText("Vælg Elev:");
        add(jLabel1);
        jLabel1.setBounds(30, 20, 80, 14);

        vaelgElevCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Ingen valgt" }));
        vaelgElevCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vaelgElevComboActionPerformed(evt);
            }
        });
        add(vaelgElevCombo);
        vaelgElevCombo.setBounds(30, 40, 190, 20);

        jLabel2.setText("Elevens Fag:");
        add(jLabel2);
        jLabel2.setBounds(30, 80, 90, 14);

        jLabel3.setText("Elevens kompetencer:");
        add(jLabel3);
        jLabel3.setBounds(510, 270, 130, 14);

        jLabel4.setText("Elevens kompetencer:");
        add(jLabel4);
        jLabel4.setBounds(-130, 190, 120, 14);

        jLabel6.setText("Tilgængelige trinmål for det valgte fag:");
        add(jLabel6);
        jLabel6.setBounds(320, 20, 220, 14);

        elevFagCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Vælg fag" }));
        elevFagCombo.setEnabled(false);
        elevFagCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                elevFagComboActionPerformed(evt);
            }
        });
        add(elevFagCombo);
        elevFagCombo.setBounds(30, 100, 150, 20);

        tilfojprotet.setText("Tilføj");
        tilfojprotet.setEnabled(false);
        tilfojprotet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tilfojprotetActionPerformed(evt);
            }
        });
        add(tilfojprotet);
        tilfojprotet.setBounds(770, 240, 70, 30);

        jLabel5.setText("det valgte trinmål til potentiale");
        add(jLabel5);
        jLabel5.setBounds(850, 250, 170, 14);

        jLabel7.setText("kompetence tilbage som potentiale");
        add(jLabel7);
        jLabel7.setBounds(600, 490, 200, 14);

        jButton2.setText("Flyt");
        jButton2.setEnabled(false);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        add(jButton2);
        jButton2.setBounds(530, 480, 70, 30);

        jButton3.setText("Slet");
        jButton3.setEnabled(false);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        add(jButton3);
        jButton3.setBounds(330, 480, 70, 30);

        jLabel8.setText("potentiale");
        add(jLabel8);
        jLabel8.setBounds(400, 490, 120, 14);

        jLabel9.setText("det valgte potentiale som kompetence");
        add(jLabel9);
        jLabel9.setBounds(100, 490, 200, 14);

        jButton4.setText("Sæt");
        jButton4.setEnabled(false);
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        add(jButton4);
        jButton4.setBounds(30, 480, 70, 30);

        jButton5.setText("Slet");
        jButton5.setEnabled(false);
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        add(jButton5);
        jButton5.setBounds(830, 480, 70, 30);

        jLabel10.setText("kompetence helt");
        add(jLabel10);
        jLabel10.setBounds(900, 490, 110, 14);

        jLabel11.setText("Elevens potentialer:");
        add(jLabel11);
        jLabel11.setBounds(10, 270, 130, 14);

        trinmaalList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        trinmaalList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                trinmaalListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(trinmaalList);

        add(jScrollPane1);
        jScrollPane1.setBounds(320, 40, 690, 190);

        kompetenceList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        kompetenceList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                kompetenceListValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(kompetenceList);

        add(jScrollPane2);
        jScrollPane2.setBounds(510, 290, 500, 180);

        potentialeList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        potentialeList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                potentialeListValueChanged(evt);
            }
        });
        jScrollPane3.setViewportView(potentialeList);

        add(jScrollPane3);
        jScrollPane3.setBounds(10, 290, 490, 180);

        priotetCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Vælg priotet", "1", "2", "3", "4", "5" }));
        priotetCombo.setEnabled(false);
        priotetCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                priotetComboActionPerformed(evt);
            }
        });
        add(priotetCombo);
        priotetCombo.setBounds(530, 240, 100, 20);
    }// </editor-fold>//GEN-END:initComponents

    private void vaelgElevComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vaelgElevComboActionPerformed
        priotetCombo.setEnabled(false);
        rydFelter();
        if (vaelgElevCombo.getSelectedIndex() != 0 && vaelgElevCombo.getItemCount() != 0) {
            Elev elev = (Elev) vaelgElevCombo.getSelectedItem();
            HoldFag[] elevensFag = controlUtil.getHoldHandler().getHoldFag(elev.getHold());
            int value = elevFagCombo.getSelectedIndex();
            elevFagCombo.removeAllItems();
            elevFagCombo.setEnabled(true);
            elevFagCombo.addItem("Vælg fag");
            for (int i = 0; i < elevensFag.length; i++) {
                elevFagCombo.addItem(elevensFag[i]);
            }
            elevFagCombo.setSelectedIndex(value);
        } else {
            elevFagCombo.setEnabled(false);
        }

    }//GEN-LAST:event_vaelgElevComboActionPerformed

    private void elevFagComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_elevFagComboActionPerformed
        rydFelter();
        priotetCombo.setEnabled(false);
        if (elevFagCombo.getSelectedIndex() != 0 && elevFagCombo.getItemCount() != 0) {
            updatekompe();
        }

    }//GEN-LAST:event_elevFagComboActionPerformed

    private void priotetComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_priotetComboActionPerformed
        if (priotetCombo.getSelectedIndex() == 0) {
            tilfojprotet.setEnabled(false);
        } else {
            tilfojprotet.setEnabled(true);
        }
    }//GEN-LAST:event_priotetComboActionPerformed

    private void tilfojprotetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tilfojprotetActionPerformed
        priotetCombo.setEnabled(false);
        Elev e = (Elev) vaelgElevCombo.getSelectedItem();
        Trinmaal t = (Trinmaal) trinmaalList.getSelectedValue();
        Potentiale pot = new Potentiale(new PotentialeId(e.getCpr(), t.getTrinId()), e, t, priotetCombo.getSelectedIndex(), false);

        controlUtil.getElevHandler().nytPotentiale(pot);
        priotetCombo.setSelectedIndex(0);
        hentElever();

        updatekompe();
    }//GEN-LAST:event_tilfojprotetActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        jButton3.setEnabled(false);
        jButton4.setEnabled(false);
        Potentiale p = (Potentiale) potentialeList.getSelectedValue();
        p.setKompetence(true);
        controlUtil.getElevHandler().opdaterPotentiale(p);
        updatekompe();

    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        jButton3.setEnabled(false);
        jButton4.setEnabled(false);
        Potentiale p = (Potentiale) potentialeList.getSelectedValue();
        controlUtil.getElevHandler().sletPotentiale(p);
        updatekompe();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        jButton2.setEnabled(false);
        jButton5.setEnabled(false);
        Potentiale p = (Potentiale) kompetenceList.getSelectedValue();
        controlUtil.getElevHandler().sletPotentiale(p);
        updatekompe();
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        jButton2.setEnabled(false);
        jButton5.setEnabled(false);
        Potentiale p = (Potentiale) kompetenceList.getSelectedValue();
        p.setKompetence(false);
        controlUtil.getElevHandler().opdaterPotentiale(p);
        updatekompe();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void trinmaalListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_trinmaalListValueChanged
        if (trinmaalList.getSelectedIndex() != -1) {


            priotetCombo.setEnabled(true);
        }
    }//GEN-LAST:event_trinmaalListValueChanged

    private void potentialeListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_potentialeListValueChanged
        if (potentialeList.getSelectedIndex() != -1) {
            jButton3.setEnabled(true);
            jButton4.setEnabled(true);
        }
    }//GEN-LAST:event_potentialeListValueChanged

    private void kompetenceListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_kompetenceListValueChanged
        if (kompetenceList.getSelectedIndex() != -1) {
            jButton2.setEnabled(true);
            jButton5.setEnabled(true);
        }
    }//GEN-LAST:event_kompetenceListValueChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox elevFagCombo;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JList kompetenceList;
    private javax.swing.JList potentialeList;
    private javax.swing.JComboBox priotetCombo;
    private javax.swing.JButton tilfojprotet;
    private javax.swing.JList trinmaalList;
    private javax.swing.JComboBox vaelgElevCombo;
    // End of variables declaration//GEN-END:variables
}
